<?php
class ToeF_Page {
	public $pagename, $pageaddr, $layout, $configuration, $lang, $charset='utf-8', $title;
	private $doctypestr;

	function __construct($pagename=null, $layout=null, $configuration=array()) {
		$this->pagename = $pagename;
		$this->layout = $layout;
		$this->configuration = $configuration;
	}

	private function loadDefaults() {
		global $config, $tpLayoutDir, $tpPageDir;
		$config = array();
		
		include APP_ROOT."application/config/config.inc.php";
		if ( !$this->layout )
			$this->layout = $config['layout'];
		if ( file_exists(APP_ROOT."$tpLayoutDir/$this->layout/config.php" ) )
			include APP_ROOT."$tpLayoutDir/$this->layout/config.php";
		if ( !$this->pagename )
			$this->pagename = 'index';
		if ( file_exists(APP_ROOT."$tpPageDir/$this->pagename.config.php"))
			include APP_ROOT."$tpPageDir/$this->pagename.config.php";
		if ( !$this->configuration )
			$this->configuration = $config;
		if ( !$this->doctypestr )
			$this->doctypestr = $this->configuration['doctype'];
		if ( !$this->lang )
			$this->lang = $this->configuration['lang'];
		if ( !$this->charset )
			$this->charset = $this->configuration['charset'];
		if ( !$this->doctypestr )
			$this->doctypestr = $this->configuration['doctype'];
		if ( !$this->title )
			$this->title = $this->configuration['title'];
	}

	public function build() {
		global $tpPageDir;

		ToeF_Page::loadDefaults();
		
		$this->pageaddr = APP_ROOT."$tpPageDir/$this->pagename";
		if ( !file_exists("$this->pageaddr.phtml") )
		{
			$errpage = new ToeF_Page($this->configuration['err_nopage'], $this->layout, $this->configuration);
			return $errpage->build();
        }
		
		if ( !$this->configuration['cache'] )
			ToeF_Page::send_nocache_header();

		$layoutclass = class_exists("ToeF_Layout_$this->layout") ? "ToeF_Layout_$this->layout" : 'ToeF_Layout';
		$layout = new $layoutclass($this->layout);

		return $layout->build($this);
	}

	public function show() {
		print ToeF_Page::build();
	}

	public function setDoctype($doctype) {
		$this->doctypestr = $doctype;
	}

	public function doctype() {
		return $this->doctypestr;
	}

	public function dtd($doctype = null) {
		if ( $doctype == null )
			$doctype = $this->doctypestr;

		if ( $doctype == null )
			return '';

		$dtdtable = array(
			"xhtml"=>"xhtml 1.0 strict",
			"xhtml1"=>"xhtml 1.0 strict",
			"xhtml1.0"=>"xhtml 1.0 strict",
			"html"=>"html 4.01 transitional",
			"xhtml-basic"=>"xhtml basic 1.0",
		);
		$addrtable = array(
			"xhtml 1.0 strict"=>"xhtml1/DTD/xhtml1-strict.dtd",
			"xhtml 1.0 transitional"=>"xhtml1/DTD/xhtml1-transitional.dtd",
			"xhtml 1.0 frameset"=>"xhtml1/DTD/xhtml1-frameset.dtd",
			"xhtml 1.1" => "xhtml11/DTD/xhtml11.dtd",
			"xhtml basic 1.0" => "xhtml-basic/xhtml-basic10.dtd",
			"xhtml basic 1.1" => "xhtml-basic/xhtml-basic11.dtd",
			"html 4.01" => "html4/strict.dtd",
			"html 4.01 transitional" => "html4/loose.dtd",
			"html 4.01 frameset" => "html4/frameset.dtd",
			"html 2.0" => null,
			"html 3.2" => null,
		);

		if ( array_key_exists($doctype, $dtdtable) )
			$doctype = $dtdtable[$doctype];
		
		if ( array_key_exists( $doctype, $addrtable ) ) {
			return sprintf("<!DOCTYPE html PUBLIC \"-//W3C//DTD %s//EN\"%s>\n", strtoupper($doctype), $addrtable[$doctype] ? "\n\"http://www.w3.org/TR/".$addrtable[$doctype]."\"" : '');
		}
		return '<!--FIXME:ERROR-->';
		//FIXME: else error	
	}

	public function html($lang=null) {
		if ( $lang == null )
			$lang = $this->lang;
		$result = '<html';
		if ( $lang )
			$result .= sprintf(' xml:lang="%s" lang="%s"', 'ko', 'ko');
		$result .= ">\n";
		return $result;
	}

	public function send_nocache_header() {
		// PREVENT CACHING FIRST BEFORE ANYTHING ELSE!
		header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
		header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); // always modified
		header("Cache-Control: no-store, no-cache, must-revalidate"); // HTTP/1.1
		header("Cache-Control: post-check=0, pre-check=0", false);
		header("Pragma: no-cache"); // HTTP/1.0
	}

	public function build_title() {
        $titlestr = '';
        if ( $this->configuration['autotitle'] )
        {
            if ( $this->configuration['grouptitle'] != '' ) {
                $titlestr .= $this->configuration['grouptitle'];
                if ( $this->title != '' )
                    $titlestr .= ' - ';
            }
            $titlestr .= $this->title;
        };
        return $titlestr ? "\t<title>$titlestr</title>\n" : '';
    }

	public function send_http_header() {

	}

	public function build_header() {
		$header ='';
		$header .= build_meta('Content-Type', 'text/html');
		$header .= build_meta('Content-Script-Type', 'text/javascript');
		if ( file_exists("$this->pageaddr.css") ) {
			$header .= import_css('../resource.php?type=page&name='.$this->pagename);
		}
		$header .= $this->build_title();
		return $header;
	}

	public function build_head() {

	}

	public function build_foot() {

	}
}
